/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaCadastroBancos.java
 *
 * Created on Jun 1, 2010, 10:58:41 AM
 */

package adcon.apresentacao;

import adcon.negocio.banco.Banco;
import adcon.persistencia.DAO;
import avant.view.AvantViewUtil;
import avant.view.model.AvantListModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Administrador
 */
public class TelaCadastroBancos extends javax.swing.JDialog {

    Banco banco;
    AvantListModel<Banco> modelo;

    /** Creates new form TelaCadastroBancos */
    public TelaCadastroBancos(AvantListModel modelo, Banco banco,java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.modelo = modelo;
        this.banco = banco;
        initComponents();
        preencherTela();
        adicionarOuvintes();
        setLocationRelativeTo(null);
       
    }

    public void adicionarOuvintes(){

        ControladorDeEventos controlador = new ControladorDeEventos();
        btnSalvar.addActionListener(controlador);
    }

    private void preencherTela(){
        if(banco != null){
        txtAgencia.setText(banco.getAgencia());
        txtNomeBanco.setText(banco.getNomeBanco());
        txtNumeroBanco.setText(banco.getNumeroBanco());
        }else{
            banco = new Banco();
        }
    }
    
    private boolean preencherObjeto(){
        if(txtAgencia.getText().equals("") | txtNomeBanco.getText().equals("") | txtNumeroBanco.getText().equals("")){
            AvantViewUtil.exibirErro("Campos não podem ficar em branco!", this);
            return false;
        }else{
            banco.setAgencia(txtAgencia.getText());
            banco.setNomeBanco(txtNomeBanco.getText());
            banco.setNumeroBanco(txtNumeroBanco.getText());
            return true;
        }
    }

    private void salvar(){
        if(preencherObjeto()){
            try {
                new DAO().salvar(banco);
                modelo.adicionar(banco);
                AvantViewUtil.exibirInformacao("Dados Adicionados com sucesso!", this);
                dispose();
            } catch (Exception e) {
                e.printStackTrace();
                AvantViewUtil.exibirErro("Não foi possível adicionar o banco!", this);
            }
       }
    }


    private class ControladorDeEventos implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            Object origem = e.getSource();

            if(origem == btnSalvar){
                salvar();
            }
        }


    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnSalvar = new javax.swing.JButton();
        txtNumeroBanco = new javax.swing.JTextField();
        txtAgencia = new javax.swing.JTextField();
        txtNomeBanco = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Bancos");
        setResizable(false);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 102, 102), 1, true));

        jLabel1.setText("Número do Banco:");

        jLabel2.setText("Nome do Banco:");

        jLabel3.setText("Agência:");

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adcon/imagens/salvar.png"))); // NOI18N
        btnSalvar.setText("Salvar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSalvar)
                    .addComponent(txtNomeBanco, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNumeroBanco, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtAgencia, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNumeroBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNomeBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnSalvar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtAgencia;
    private javax.swing.JTextField txtNomeBanco;
    private javax.swing.JTextField txtNumeroBanco;
    // End of variables declaration//GEN-END:variables

}
